<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShowDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="btnEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="pageSetting.pagesize" @current-change="currentChange" /></el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" /></el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog :title="`${formData.id?'编辑':'新增'}角色`" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="addRole" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" /></el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" /></el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="addRoleClick">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
      <el-dialog title="分配权限列表" :visible="isShowPermission">
        <el-checkbox-group v-model="checkedPerm">
          <el-tree :data="permissionList" :props="{label:'name'}" default-expand-all :expand-on-click-node="false">
            <template #default="{data}">
              <!-- 每个属性节点都是一个多选框 -->
              <el-checkbox :label="data.id">
                {{ data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo } from '@/api/company.js'
import { getRoleList, delRole, addRole, getRoleDetailById, updateRole } from '@/api/setting'
import { getPermissionList, assignPerm } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      permId: '',
      checkedPerm: [],
      permissionList: [], // 用来接受权限列表的请求数据
      isShowPermission: false,
      companyInfo: {}, // 用于存放请求回来的数据
      roleList: [],
      total: 0, // 总页数
      pageSetting: {
        page: 1, // 页码
        pagesize: 2 // 每页条数
      },
      isShowDialog: false, // 控制弹窗显示隐藏
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.permissionList = listToTreeData(await getPermissionList(), '0')
  },
  methods: {
    // 分配权限确定按钮
    async btnPermOK() {
      await assignPerm({ id: this.permId, permIds: this.checkedPerm })
      this.$message.success('操作成功')
      this.isShowPermission = false
    },
    // 分配权限取消按钮

    btnPermCancel() {
      this.checkedPerm = []
      this.isShowPermission = false
    },
    // 分配权限功能
    async assignPerm(id) {
      const { permIds } = await getRoleDetailById(id)
      this.checkedPerm = permIds
      this.permId = id
      this.isShowPermission = true
    },
    // 根据id查询公司接口
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.profile.companyId)
    },
    // 获取所有角色列表接口
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageSetting)
      this.roleList = rows
      this.total = total
    },
    // 根据点击的页数切换数据
    currentChange(newPage) {
      // console.log(90, newPage)    //newPage是当前点击的页数
      this.pageSetting.page = newPage
      this.getRoleList()
    },
    // 删除功能
    async deleteRole(id) {
      try {
        // 二次询问
        await this.$confirm('确认要删除吗')
        // console.log(id)
        // 发送删除请求
        await delRole(id)
        // 提示用户操作结果
        this.$message.success('删除成功！')
        // 重新渲染
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 确定
    async addRoleClick() {
      // console.log(1)
      // 校验数据
      await this.$refs.addRole.validate()
      // 发请求   判断是新增还是编辑 区别在于有没有id
      if (this.formData.id) {
        // 编辑
        await updateRole(this.formData)
      } else {
        // 新增
        await addRole(this.formData)
      }

      // 提示用户
      this.$message.success('操作成功！')
      // 关闭弹窗
      this.isShowDialog = false
      // 渲染数据
      this.getRoleList()
    },
    // 取消按钮
    btnCancel() {
      // 清理绑定过的数据，还有表单报错
      // resetFields只能清理绑定过的默认数据
      // 需要手动清理其它数据才行
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.addRole.resetFields()
      this.isShowDialog = false
    },
    // 编辑功能
    async btnEdit(id) {
      // console.log(id)
      this.formData = await getRoleDetailById(id)
      // 显示弹窗
      this.isShowDialog = true
    }
  }
}
</script>

<style lang="less" scoped>

</style>
